﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Collections.Specialized;
using System.IO;
using System.Configuration;

namespace Util
{
    public class ApplicationConfig
    {
        private ApplicationSettingsBase appSettings;

        private string localConnectionString;
        private string corpConnectionString;
        private bool isFirstRun;
        private string printer;
        private bool enableElston;
        private bool useServerLabels;
        private string serverLabelLocation;
        private bool printRehangs;
        private string connectionString;

        [CategoryAttribute("Connection Strings")]
        public string LocalConnectionString
        {
            get { return localConnectionString; }
            set { localConnectionString = value; }
        }

        [CategoryAttribute("Connection Strings")]
        public string CorpConnectionString
        {
            get { return corpConnectionString; }
            set { corpConnectionString = value; }
        }

        [CategoryAttribute("Connection Strings")]
        public String ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }

        [CategoryAttribute("Labels")]
        public bool UseServerLabels
        {
            get { return useServerLabels; }
            set { useServerLabels = value; }
        }

        [CategoryAttribute("Labels")]
        public string ServerLabelLocation
        {
            get { return serverLabelLocation; }
            set { serverLabelLocation = value; }
        }

        [CategoryAttribute("Print")]
        public bool PrintRehangs
        {
            get { return printRehangs; }
            set { printRehangs = value; }
        }

        public bool IsFirstRun
        {
            get { return isFirstRun; }
            set { isFirstRun = value; }
        }

        [TypeConverter(typeof(FileNameConverter))]
        public string Printer
        {
            get { return printer; }
            set { printer = value; }
        }

        public string PrintXml
        {
            get{ return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "POSPlus\\print.xml"); }
        }

        public bool EnableElston
        {
            get { return enableElston; }
            set { enableElston = value; }
        }

        public ApplicationConfig(ApplicationSettingsBase appSettings)
        {
            this.appSettings = appSettings;
            this.localConnectionString = (String) appSettings["LocalConnectionString"];
            this.corpConnectionString = (String) appSettings["CorpConnectionString"];
            this.isFirstRun = (bool)appSettings["IsFirstRun"];
            this.printer = (String)appSettings["Printer"];
            this.enableElston = (bool)appSettings["EnableElston"];
            this.useServerLabels = (bool)appSettings["UseServerLabels"];
            this.serverLabelLocation = (String)appSettings["ServerLabelLocation"];
            this.printRehangs = (bool)appSettings["PrintRehangs"];
            this.connectionString = (String)appSettings["ConnectionString"];
        }

        public void Save()
        {
            appSettings["LocalConnectionString"] = localConnectionString;
            appSettings["CorpConnectionString"] = corpConnectionString;
            appSettings["IsFirstRun"] = isFirstRun;
            appSettings["Printer"] = printer;
            appSettings["EnableElston"] = enableElston;
            appSettings["UseServerLabels"] = useServerLabels;
            appSettings["ServerLabelLocation"] = serverLabelLocation;
            appSettings["PrintRehangs"] = printRehangs;
            appSettings["ConnectionString"] = connectionString;
            appSettings.Save();
        }

    }

    public class FileNameConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<string> printers = new List<string>();
            foreach (string printer in PrinterSettings.InstalledPrinters)
            {
                printers.Add(printer);
            }


            return new StandardValuesCollection(printers.ToArray());
        }
    }
}
